package luminus.origin.commands.rewrite

import luminus.origin.utils.send
import taboolib.common.platform.ProxyCommandSender
import taboolib.common.platform.ProxyPlayer
import taboolib.common.platform.command.*
import taboolib.common.platform.function.adaptPlayer
import taboolib.module.lang.asLangText
import taboolib.module.lang.sendLang

@CommandHeader("whisper", ["w", "tell", "msg"], permission = "luminus.whisper", permissionDefault = PermissionDefault.TRUE)
object Whisper {

    val reply = HashMap<ProxyPlayer, ProxyPlayer>()

    @CommandBody
    val main = mainCommand {
        player("target") {
            dynamic("message") {
                execute<ProxyCommandSender> { sender, context, _ ->
                    val target = context.player("target")
                    val message = context.player("message")
                    if (target.name == sender.name) {
                        sender.sendLang("commands-whisper-errorTarget")
                    } else {
                        sender.send(sender.asLangText("commands-whisper-whisperSend").format(target.name, message))
                        target.send(sender.asLangText("commands-whisper-whisperReceive").format(sender.name, message))
                        reply[adaptPlayer(sender)] = target
                        reply[target] = adaptPlayer(sender)
                    }
                }
            }
        }
    }
}